<template>
  <section>
    <!-- 产品列表 -->
    <a-table
      :dataSource="tableData"
      :columns="tableColumns"
      :rowKey="(record) => record.s_a_id"
      :customRow="customTableRow"
      :pagination="false"
      bordered
    >
      <template slot="pay_unit" slot-scope="row">
        {{ getLabel(row.pay_unit, [], 'pay_unit') }}
      </template>
      <template slot="pay_unit_base" slot-scope="row">
        {{ getLabel(row.pay_unit_base, [], 'pay_unit_base') }}
      </template>
      
      <!-- 按钮区 -->
      <template slot="action" slot-scope="row">
        <a-button
          v-if="hasEditProject"
          type="link"
          size="small"
          @click="handleTableEdit(row)"
          >修改</a-button
        >

        <!-- <a-button
          v-if="isLarge && hasEditProject"
          class="action-button"
          type="link"
          size="small"
          @click="handlerBatch(row)"
          >分批发放</a-button
        > -->

        <!-- <a-popconfirm
          :title="`您确认锁定${row.s_a_nam}的补贴核算方式?锁定后无法修改`"
          ok-text="确认"
          cancel-text="取消"
          @confirm="handlerLock(row)"
        >
          <a-button
            v-if="!row.islock"
            class="action-button"
            type="danger"
            size="small"
          >锁定</a-button>
        </a-popconfirm> -->
      </template>
    </a-table>

    <!-- 产品列表分页器 -->
    <!-- <a-pagination
      slot="paging"
      :pageSizeOptions="pageSizes"
      :total="total"
      showSizeChanger
      :pageSize="pageSize"
      v-model="currentPage"
      @showSizeChange="changePageSize"
      @change="changePage"
    >
    </a-pagination> -->

    <!-- 编辑产品数据 -->
    <a-modal
      :visible="visible"
      :title="visibleTitle"
      centered
      :width="600"
      :footer="null"
      @cancel="visible = false"
      @ok="handleSubmit"
    >
      <custom-form
        ref="custom-form"
        class="custom-form"
        v-model="currentRow"
        :FormList="formColumns"
        :mode="formMode"
        v-bind="formItemLayout"
        layout="horizontal"
      >
        <div style="display: flex; margin: 20px 0 20px 50%;">
          <a-button @click="handleSubmit" type="primary">确定</a-button>
          <a-button @click="visible = false" style="margin-left: 10px;">取消</a-button>
        </div>
      </custom-form>

      <div style="border-top: 1px solid #ddd;margin-top:20px" v-if="isLarge && hasEditProject && currentYear > 2022">
        <Batch :sid="currentRow.s_a_id"/>
      </div>
      
      <!-- <section slot="footer"> -->
      <!-- </section> -->
    </a-modal>
  </section>
</template>

<script lang="ts">
import { Mixins, Component, Watch, Vue, Prop } from 'vue-property-decorator'
import { State, Mutation, Getter, namespace } from 'vuex-class'
import EventsMixin from '@/mixin/events'
import { TableMixin, AuthMixin } from '@/mixin'

import { TableLayout } from '@/layouts'
import CustomForm from '@/components/src/Form'
import { userServices, IAdmin } from '@/common/services'
import { userModule } from '@/store/modules/user'
import { commonServices } from '@/common/services/common'
import { memberRole, PayUnitType, PayUnitTypeEnum } from '@/common/enum'
import { subsidyServices, ISubsidyStandard } from '@/common/services/subsidy'
import Batch from './batch.vue'

@Component({
  components: {
    Layout: TableLayout,
    CustomForm,
    Batch
  },
})
export default class AdminList extends Mixins(
  EventsMixin,
  TableMixin,
  AuthMixin
) {
  @Prop({
    type: Array,
  })
  public standardData!: ISubsidyStandard[]

  get hasEditProject() {
    return userModule.hasEditProject
  }

  get currentYear() {
    return userModule.currentYear
  }

  get isLarge() {
    return userModule.isLarge
  }

  public commonParams = {
    s_a_id: '',
  }

  public formItemLayout = {
    labelCol: { span: 8 },
    wrapperCol: { span: 16 },
  }

  // 产品列表搜索条件
  public tableSearch = {}

  // 产品列表表格信息
  public columns = [
    {
      title: '行政编码',
      dataIndex: 's_a_id',
      key: 's_a_id',
      width: 80,
      form: {
        type: 'a-input',
        placeholder: '请输入行政编码',
        disabled: true,
      },
    },
    {
      title: '行政区名称',
      dataIndex: 's_a_nam',
      key: 's_a_nam',
      width: 100,
      form: {
        type: 'a-input',
        placeholder: '请输入行政区名称',
        disabled: true,
      },
    },

    // @ts-ignore
    ...(this.isLarge
      ? []
      : [{
          title: '基础面积依据',
          key: 'pay_unit_base',
          width: 180,
          form: {
            type: 'a-select',
            placeholder: '请选择基础面积依据',
            dict: PayUnitType.filter(item => item.value !== PayUnitTypeEnum.FIELD).map((item) => {
              let label = item.label

              switch (item.value) {
                case '1':
                  label = item.label + '(粮食种植面积)'
                  break
                case '3':
                  label = item.label + '(二轮承包面积)'
                  break
                case '4':
                  label = item.label + '(计税耕地面积)'
                  break
                case '5':
                  label = item.label + '(确权耕地面积)'
                  break
                // case '9':
                //   label = '其他面积'
                //   break
              }

              return {
                label,
                value: item.value,
              }
            }),
            // .concat([
            //   {
            //     label: '无',
            //     value: ''
            //   }
            // ]),
            isShowList: !userModule.isPlant,
            isShowFrom: !userModule.isPlant,
            customDisabled: (_: any) => Boolean(_.islock),
          },
          scopedSlots: { customRender: 'pay_unit_base' },
        }]),
    // {
    //   title: '补贴核算依据',
    //   key: 'pay_unit',
    //   width: 180,
    //   form: {
    //     type: 'a-select',
    //     placeholder: '请选择补贴核算依据',
    //     dict: PayUnitType.filter((item) =>
    //       this.isLarge ? [1, 2].includes(+item.value) : true
    //     ).map((item) => {
    //       let label = ''

    //       switch (item.value) {
    //         case '1':
    //           label = item.label + '(粮食种植面积)'
    //           break
    //         case '2':
    //           label = item.label + '(实际耕种面积)'
    //           break
    //         case '3':
    //           label = item.label + '(二轮承包面积)'
    //           break
    //         case '4':
    //           label = item.label + '(计税耕地面积)'
    //           break
    //         case '5':
    //           label = item.label + '(确权耕地面积)'
    //           break
    //       }

    //       return {
    //         label,
    //         value: item.value,
    //       }
    //     }),
    //     isShowList: !userModule.isPlant,
    //     isShowFrom: !userModule.isPlant,
    //     customDisabled: (_: any) => Boolean(_.islock),
    //   },
    //   scopedSlots: { customRender: 'pay_unit' },
    // },

    {
      title: '补贴计算标准（元/亩）',
      dataIndex: 'pay_price',
      key: 'pay_price',
      width: this.isLarge ? 160 : 120,
      align: 'center',
      form: {
        type: 'a-input',
        placeholder: '请输入',
        customDisabled: (_: any) => Boolean(_.islock),
        // tips: '数值必须大于或等于0；大于0时，锁定辖区全部补贴数据。',
        validate: [
          {
            required: true,
            message: '请输入正确的补贴计算标准',
            pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/,
            transform(value: string) {
              return +value
            },
            trigger: 'change',
          },
        ],
      },
    },
    // {
    //   title: '锁定',
    //   key: 'pay_unit',
    //   width: 110,
    //   form: {
    //     type: 'a-checkbox',
    //     placeholder: '请选择补贴核算依据',
    //     dict: PayUnitType
    //   },
    //   scopedSlots: { customRender: 'pay_unit' }
    // },
    {
      title: '操作',
      key: 'action',
      width: 60,
      scopedSlots: { customRender: 'action' },
    },
  ]

  public tableApi = {
    edit: subsidyServices.updateSubsidyStandard,
  }

  @Watch('standardData', { immediate: true })
  public handlerStandardDataChange() {
    this.tableData = this.standardData
  }

  public getData() {
    this.$emit('update')
  }

  public async handleTableEdit(row: ISubsidyStandard) {
    this.tableEdit(Object.assign({}, row))
    this.visibleTitle = '补贴方式管理'
  }

  // 锁定
  public handlerLock(row: ISubsidyStandard) {
    subsidyServices
      .updateSubsidyStandard({
        ...row,
        islock: !row.islock,
      })
      .then((_) => {
        this.$message.success('锁定成功')
        this.getData()
      })
  }

  public handlerBatch(row: any) {
    this.$dialog(
      Batch,
      {
        sid: row.s_a_id
      },
      {
        title: '拆分发放批次',
        footer: null,
        centered: true
      }
    )
  }
}
</script>

<style scoped>
.action-button {
  margin: 0px 10px 0px 10px;
}
.form-item-class {
  margin-bottom: 0px;
}
.img-box {
  width: 100px;
  height: 100px;
  margin: 10px;
}
.item-blue {
  color: #1989fa;
}
</style>
